﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterAdmin.master" AutoEventWireup="true" CodeBehind="Invoice.aspx.cs" Inherits="Project_Udrinkwater.PagesAdmin.Invoice" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <dx:ASPxRoundPanel ID="RPN_SalaryEmp" runat="server" Width="100%" 
         HeaderText="รายการส่งของ">
        <HeaderStyle Font-Size="Medium" HorizontalAlign="Left" /></dx:ASPxRoundPanel>
    <script type="text/javascript">
    // <![CDATA[
        var lastCountry = null;
        function OnCountryChanged(cmbCountry) {
            if ( grid.GetEditor("City").InCallback())
                lastCountry = cmbCountry.GetValue().toString();
            else
                grid.GetEditor("City").PerformCallback(cmbCountry.GetValue().toString());
        }
        function OnEndCallback(s, e) {
            if (lastCountry) {
                grid.GetEditor("City").PerformCallback(lastCountry);
                lastCountry = null;
            }
        }
    // ]]>
    </script>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="พิมพ์รายงาน" />
    <dx:ASPxGridView ID="ASPxGridViewMaster" ClientInstanceName="grid" runat="server" 
        AutoGenerateColumns="False" DataSourceID="SqlDataInvoice" 
        KeyFieldName="Invoice_ID" Width="100%">
        <Columns>
            <dx:GridViewCommandColumn VisibleIndex="13" Caption="ทำรายการ" 
                ShowSelectCheckbox="True">
                <EditButton Text="แก้ไข" Visible="True">
                </EditButton>
                <NewButton Visible="True" Text="เพิ่ม">
                </NewButton>
                <DeleteButton Text="ลบ" Visible="True">
                </DeleteButton>
                <ClearFilterButton Visible="True">
                </ClearFilterButton>
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn FieldName="Invoice_ID" ReadOnly="True" 
                VisibleIndex="0" Visible="False">
                <EditFormSettings Visible="False" />
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="RowID" ReadOnly="True" 
                VisibleIndex="1" Caption="ลำดับ" Width="10px">
                <EditFormSettings Visible="False" />
                <HeaderStyle HorizontalAlign="Center" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Invoice_doc_NO" VisibleIndex="2" 
                Caption="เลขที่" ReadOnly="True">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataComboBoxColumn Caption="เลขที่ใบสั่ง" FieldName="Order_ID" 
                VisibleIndex="3">
                <PropertiesComboBox DataSourceID="SqlDataOder" TextField="Order_doc_NO" 
                    ValueField="Order_ID" ValueType="System.Int32" 
                    IncrementalFilteringMode="Contains">
                </PropertiesComboBox>
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataDateColumn FieldName="Deadlines_Date" VisibleIndex="4" 
                Caption="วันที่ส่ง">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataDateColumn>
            <dx:GridViewDataTextColumn FieldName="Order_doc_NO" VisibleIndex="5" 
                ReadOnly="True" Caption="เลขที่ใบสั่ง" Visible="False">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="รหัสลูกค้า" FieldName="Customer_ID" 
                VisibleIndex="6">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Cutomer" ReadOnly="True" VisibleIndex="7" 
                Caption="ชื่อ - นามสกุล">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Destination" ReadOnly="True" 
                VisibleIndex="8" Caption="ที่อยู่">
                <EditFormSettings Visible="False" />
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataComboBoxColumn Caption="พนักงานส่ง" FieldName="Employee_ID" 
                VisibleIndex="9">
                <PropertiesComboBox DataSourceID="SqlDataEmpID" TextField="FullName" 
                    ValueField="Employee_ID">
                </PropertiesComboBox>
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataCheckColumn FieldName="Invoice_Accep" 
                VisibleIndex="10" Caption="ยืนยันการส่ง">
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataCheckColumn>
            <dx:GridViewDataCheckColumn Caption="ยืนยันการชำระเงิน" FieldName="Money_Accep" 
                VisibleIndex="11">
                <CellStyle HorizontalAlign="Center">
                </CellStyle>
            </dx:GridViewDataCheckColumn>
            <dx:GridViewDataTextColumn Caption="พิมพ์รายการ" VisibleIndex="12">
                <EditFormSettings Visible="False" />
                <DataItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" 
                        CommandArgument ='<%# Eval("Invoice_ID")%>' oncommand="LinkButton1_Command">พิมพ์ใบส่ง</asp:LinkButton>
                </DataItemTemplate>
            </dx:GridViewDataTextColumn>
        </Columns>
        <SettingsBehavior ConfirmDelete="True" />
        <Settings ShowFilterRow="True" />
        <SettingsText ConfirmDelete="ยืนยันการลบ" />
        <SettingsDetail ShowDetailRow="True" />
        <Templates>
            <DetailRow>
                <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" 
                    DataSourceID="SqlDataOrder_List" 
                    onbeforeperformdataselect="ASPxGridView1_BeforePerformDataSelect" 
                    Width="100%">
                    <TotalSummary>
                        <dx:ASPxSummaryItem DisplayFormat="รวม {0:#,###.00}" FieldName="Total" 
                            ShowInColumn="Total" ShowInGroupFooterColumn="Total" SummaryType="Sum" />
                    </TotalSummary>
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="Order_ID" VisibleIndex="0" 
                            Visible="False">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Product_Name" VisibleIndex="3" 
                            Caption="ชื่อสินค้า">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Left">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Product_ID" VisibleIndex="1" 
                            Visible="False">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Amount" VisibleIndex="4" Caption="จำนวน">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Price_Unit" VisibleIndex="5" 
                            Caption="ราคาต่อหน่วย">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Discount" VisibleIndex="6" 
                            Caption="ส่วนลด">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Total" VisibleIndex="7" Caption="รวม" 
                            ReadOnly="True">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Right">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn Caption="ลำดับ" FieldName="RowID" VisibleIndex="2">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center">
                            </CellStyle>
                        </dx:GridViewDataTextColumn>
                    </Columns>
                    <Settings ShowFooter="True" />
                </dx:ASPxGridView>
            </DetailRow>
        </Templates>
    </dx:ASPxGridView>
    <asp:SqlDataSource ID="SqlDataInvoice" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        
        SelectCommand="SELECT        TB_Invoice.[Invoice_ID]
			  ,ROW_NUMBER() OVER (ORDER BY TB_Invoice.Invoice_ID ASC) AS RowID
			  ,TB_Invoice.[Invoice_doc_NO]
			  ,TB_Order.[Deadlines_Date]
			  ,TB_Invoice.[Order_ID]
              ,TB_Invoice.[Invoice_Accep]
              ,TB_Invoice.Employee_ID
              ,TB_Invoice.[Money_Accep]
              ,TB_Customer.Customer_ID
              , TB_Customer.Firstname + ' ' + TB_Customer.Lastname AS Cutomer
              ,  TB_Order.DesAddres AS Destination            
FROM          TB_Invoice INNER JOIN
              TB_Order ON TB_Invoice.Order_ID = TB_Order.Order_ID INNER JOIN
              TB_Customer ON TB_Order.Customer_ID = TB_Customer.Customer_ID INNER JOIN
              TB_Amphur ON TB_Customer.AMPHUR_ID = TB_Amphur.AMPHUR_ID INNER JOIN
              TB_District ON TB_Customer.DISTRICT_ID = TB_District.DISTRICT_ID AND TB_Amphur.AMPHUR_ID = TB_District.AMPHUR_ID INNER JOIN
              TB_Province ON TB_Customer.PROVINCE_ID = TB_Province.PROVINCE_ID AND TB_Amphur.PROVINCE_ID = TB_Province.PROVINCE_ID AND TB_District.PROVINCE_ID = TB_Province.PROVINCE_ID" 
        DeleteCommand="DELETE FROM TB_Invoice WHERE (Invoice_ID = @Invoice_ID)" 
        
        
        
        
        
        InsertCommand="INSERT INTO TB_Invoice(Order_ID, Employee_ID, Invoice_Accep, Money_Accep) VALUES (@Order_ID, @Employee_ID, @Invoice_Accep, @Money_Accep)" 
        
        
        
        UpdateCommand="UPDATE TB_Invoice SET Order_ID = @Order_ID, Employee_ID = @Employee_ID, Invoice_Accep = @Invoice_Accep, Money_Accep = @Money_Accep WHERE (Invoice_ID = @Invoice_ID)">
        <DeleteParameters>
            <asp:Parameter Name="Invoice_ID" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Order_ID" />
            <asp:Parameter Name="Employee_ID" />
            <asp:Parameter Name="Invoice_Accep" />
            <asp:Parameter Name="Money_Accep" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Order_ID" />
            <asp:Parameter Name="Employee_ID" />
            <asp:Parameter Name="Invoice_Accep" />
            <asp:Parameter Name="Money_Accep" />
            <asp:Parameter Name="Invoice_ID" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataEmpID" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
    
        SelectCommand="SELECT Employee_ID, FirstName + ' ' + LastName AS FullName FROM TB_Employee"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataCusID" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        SelectCommand="SELECT Customer_ID FROM TB_Customer"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataOder" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        SelectCommand="SELECT [Order_ID], [Order_doc_NO] FROM [TB_Order]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataOrder_List" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        
        
        SelectCommand="SELECT TB_Order_List.id,	
	TB_Order_List.Order_ID,
				ROW_NUMBER() OVER (ORDER BY TB_Order_List.id ASC) AS RowID, 
				TB_Product.Product_Name, 
			  TB_Order_List.Product_ID, 
			  TB_Order_List.Amount, 
			  TB_Order_List.Price_Unit, 
			  TB_Order_List.Discount,
			  ( TB_Order_List.Amount * TB_Order_List.Price_Unit ) - TB_Order_List.Discount as Total FROM TB_Order_List INNER JOIN TB_Product ON TB_Order_List.Product_ID = TB_Product.Product_ID WHERE (TB_Order_List.Order_ID = @id)">
        <SelectParameters>
            <asp:SessionParameter Name="id" SessionField="Order_ID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <dx:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server" 
        ExportedRowType="Selected" GridViewID="ASPxGridViewMaster" 
        Landscape="True" MaxColumnWidth="600">
        <Styles>
            <Default Font-Names="Angsana New">
            </Default>
        </Styles>
        <PageHeader Right="[Pages #]
[Date Printed]">
        </PageHeader>
    </dx:ASPxGridViewExporter>
</asp:Content>
